package movie.controller;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.CinemaName;
import movie.service.CinemaNameService;

@Controller
public class CinemaLoginController {
	@Autowired
	private CinemaNameService cinemaNameService;
	
	@RequestMapping(value = "/cwelcome", method = RequestMethod.GET)
	public String get3(HttpServletRequest request) {
		return "forward:cwelcome.jsp";
	}
	
	@RequestMapping(value = "/clogin", method = RequestMethod.GET)
	public String get(HttpServletRequest request, Model model) {
		model.addAttribute("clogin_error", 0);
		HttpSession session = request.getSession();
		if(session.getAttribute("cinemaId") != null) {
			return "redirect:/cinemaFindAll";
		}
		else return "forward:clogin.jsp";
	}
	@RequestMapping(value = "/clogoff", method = RequestMethod.GET)
	public String get2(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute("cinemaId");
		return "redirect:clogin.jsp";
	}
	@RequestMapping(value = "/clogin", method = RequestMethod.POST)
	public String post(CinemaName cinema,HttpServletRequest request, Model model) {
//		System.out.println("cinema:" + cinema.toString());
		CinemaName cinema1 = cinemaNameService.Login(cinema);
//		System.out.println("cinema1:" + cinema1.toString());
		if(cinema1 != null) {//成功
			if(cinema1.getCinemaPassword().equals(cinema.getCinemaPassword())) {
				HttpSession session = request.getSession();
	            session.setAttribute("cinemaId", cinema1.getCinemaId());
//	            session.setMaxInactiveInterval(15);
	            model.addAttribute("clogin_error", 0);
				return "redirect:/cinemaFindAll";
			}
			else{//密码错误
				model.addAttribute("clogin_error", 1);
				return "forward:clogin.jsp";//重定向
			}
		}

		else {//未注册
			model.addAttribute("clogin_error", 1);
			return "forward:clogin.jsp";
		}
		//forward
	}
}
